{extend name="public/nav" /}
{block name="title"} 档案管理系统 {/block}

{block name="content"}
<style>
    html,body{
        height: 100%;
    }
    input{
        border: none;
    }
    .layui-input-block{
        margin-left: 0px;
    }
    .layui-form-select dl dd.layui-this {
        background-color: #1296db;
        color: #fff;
    }
    #img img{
        width: 120px;
        height: 120px;
        border-radius: 250px;
    }
    .layui-btn-color{
        background-color: transparent;
        color: #2D93CA;
        font-weight: bold;
        display: block;
        width: 100%;
        border: 1px solid #1296db;
    }
</style>
<div class="site-content" style="background-color: #cccccc;">
    <div class="site-h1">
        <i class="layui-icon layui-icon-left" onclick="window.location.href='/'"></i>添加用户
    </div>
    <div class="layui-container">
        <div class="site-text" style="margin-top: 30px;">
            <form class="layui-form" method="post">
                <div class="layui-form-item">
                    <div>用户名</div>
                    <div class="layui-input-block">
                        <input type="text" name="user_name" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div>密码</div>
                    <div class="layui-input-block">
                        <input type="password" name="user_pass" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div>头像</div>
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn layui-btn-color" id="test1">
                            <i class="layui-icon">&#xe67c;</i>上传
                        </button>
                        <input type="hidden" name="user_face" placeholder="" autocomplete="off" class="layui-input">
                    </div>
                    <div id="img" style="display: none;margin-top: 10px;">
                        <img src="/uploads/20200505/24141fadfda9fef8cf77cad87a81c2a6.png" alt="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div>工作区</div>
                    <div class="layui-input-block">
                        <select name="region_id">
                            {foreach $region as $r}
                            <option value="{$r.id}">{$r.region_name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-color" lay-submit lay-filter="add">保存</button>
                    </div>
                </div>
            </form>

            <script type="text/javascript">
                layui.use(['form','upload','jquery'],function () {
                    var form = layui.form,upload = layui.upload,layer = layui.layer,$ = layui.jquery;
                    //上传
                    upload.render({
                        elem: '#test1' //绑定元素
                        ,url: '{:url("index/api/upload")}' //上传接口
                        ,done: function(res){
                            //上传完毕回调
                            if (res.code == 0){
                                layer.msg('上传成功')
                                $('input[name=user_face]').val(res.data.path);
                                $('#img').html('<img src="'+res.data.path+'" />');
                                $('#img').show();
                            }else{
                                layer.msg('上传失败')
                            }
                        }
                        ,error: function(){
                            layer.msg('上传失败');
                        }
                    });

                    //提交
                    form.on('submit(add)',function (data) {
                        $.post('{:url("index/admin/add")}',data.field,function (res) {
                            if (res.code == 0){
                                layer.msg('保存成功',{icon:6,time:1000},function () {
                                    window.location.href = '{:url("index/admin/index")}';
                                })
                            }else{
                                layer.msg('保存失败');
                            }
                        },'json');
                        return false;
                    });
                })
            </script>
        </div>
    </div>
</div>
{/block}